#!/usr/bin/env bash
#
# --------------------------------------------------------------
# WARNING: This script is not quite working properly for me.
# The rsync '--compare-dest' option appears not to work as
# I expect. Files with a small change in them appear to be fully
# transferred instead of getting rsync-d with the previous copy
# in the old current directory as a basis.
#
# This script is included so that it can be fixed at a later point.
# On the positive side, if you use hardlinked_archive=1 in your
# server config, the hardlinks are transferred efficiently, and
# only changed files are transferred whole.
# --------------------------------------------------------------
#
#
# This script demonstrates how to run efficient offsite rsyncs using the burp
# server side post script mechanism.
# You need to place something like this in the server config file:
# server_script_post = /etc/burp/offsite-backup
# server_script_post_run_on_fail = 0
# This can be given globally, or differently for individual clients.

prog=$(basename $0)

preorpost="$1" ; shift
action="$1" ; shift	# either "backup" or "backup_timed"
client="$1" ; shift
success="$1" ; shift
timer="$1" ; shift

# You will neeed to adjust the following variables as appropriate (or turn
# them into parameters passed in via 'server_script_post_arg = ' options in
# the config files).

# The storage directory for the client on the burp server side.
src="/var/spool/burp/$client/"
# The user/address of the remote site.
dst="graham@10.253.0.13:"
# The directory to update at the remote site.
rdir="~/remote-backup/$client"
# The private ssh key on the local machine that has access to the remote site.
private_key="/home/graham/.ssh/id_rsa"

usage()
{
	echo "usage: $prog post [backup|backup_timed] <client> [0|1] [0|1]" 1>&2
	echo "The third argument is whether the backup succeeded (0 for ok)" 1>&2
	echo "The fourth argument is whether the timer script allowed a backup (0 for ok)" 1>&2
	exit 1
}

if   [ -z "$preorpost" -o -z "$action" -o -z "$client" -o -z "$success" -o -z "timer" ] || [ "$preorpost" != "post" ]
then
	usage
fi

if [ "$action" != "backup" -a "$action" != "backup_timed" ] ; then
	# It was not a backup that ran.
	exit 0
fi

if [ "$action" = "backup_timed" ] && [ "$timer" != "0" ] ; then
	# Server did not allow timed backup to be attempted.
	exit 0
fi

if [ "$success" != "0" ] ; then
	# Backup failed - do not run the offsite backup.
	exit 0
fi

# Needs two passes.
# The previous 'curent' directory on the server side can be utilised to save
# time. 'current' is a symlink pointing to the latest backup.

# The first pass is to upload all the data, using the remote 'current'
# directory as a reference. Do not include symlinks in this pass, so that
# 'current' is not updated.

if ! /usr/bin/rsync \
		--verbose \
		--recursive \
		--perms \
		--times \
		--compare-dest="$rdir/current/" \
		--hard-links \
		--delay-updates \
		--delete-after \
		-e "ssh -i $private_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" \
		"$src" \
		"$dst$rdir"
then
	echo "Phase 1 of remote sync failed for $client."
	exit 1
fi

# Phase two is to only include the 'current' symlink. It gets moved at the
# end when everything else is in place.

if ! /usr/bin/rsync \
		--verbose \
		--links \
		-e "/usr/bin/ssh -i $private_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" \
		"$src/current" \
		"$dst$rdir"
then
	echo "Phase 2 of remote sync failed for $client."
	exit 1
fi


exit 0
